﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using mogco.Properties;
using mogco.Structs;
using System.Xml.Serialization;
using System.Drawing;

namespace mogco
{
    public static class Settings
    {

        public static SerializableSettings xml = new SerializableSettings();

        public static void Initialize()
        {
            if (!File.Exists("settings.xml"))
            {
                Settings.CreateSettingsFile();
                Settings.LoadSettings();
            }
            else
                Settings.LoadSettings();     
        }

        private static void CreateSettingsFile()
        {
            SerializableSettings settingStruct = new SerializableSettings();
            settingStruct.JPGQuality = 3;
            settingStruct.UseFreeImage = true;
            settingStruct.Resolution = new Point(240, 320);
            // default resolution settings
            settingStruct.ResolutionArray = new List<Point>() { new Point(240, 320), new Point(480, 640) };
            settingStruct.OpenPictureExtensions = mogco.Structs.Structs.PictureExtensions.JPG;
            settingStruct.SaveFormat = mogco.Structs.Structs.PictureExtensions.JPG;
            XmlSerializer xs = new XmlSerializer(typeof(SerializableSettings));
            XmlTextWriter settingsWriter = new XmlTextWriter("settings.xml", Encoding.UTF8);
            xs.Serialize(settingsWriter, settingStruct);
            settingsWriter.Close();
            // Кодировочку по правилам
        }

        private static void LoadSettings()
        {
            XmlSerializer xs;
            try
            {
                xs = new XmlSerializer(typeof(SerializableSettings));
            }
            catch (Exception e)
            {
                Logger.txtLogger.WriteMessage("Error loading settings." + e.ToString());
                Settings.CreateSettingsFile();
                return;
            }
            if (File.Exists("settings.xml"))
            {
                FileStream fs = new FileStream("settings.xml", FileMode.Open, FileAccess.ReadWrite);
                xml = (SerializableSettings)xs.Deserialize(fs);
                fs.Close();
            }
            else
            {
                Logger.txtLogger.WriteMessage("Setting file not exists");
            }
        }

        public static void Save()
        {
            XmlSerializer xs = new XmlSerializer(typeof(SerializableSettings));
            try
            {
                XmlTextWriter settingsWriter = new XmlTextWriter("settings.xml", Encoding.UTF8);
                xs.Serialize(settingsWriter, xml);
                settingsWriter.Close();
            }
            catch (Exception e)
            {
                Logger.txtLogger.WriteMessage("Error with setting file " + e.ToString());
            }
        }

        public static void Reload()
        {
            Settings.LoadSettings();
        }
    }
}
